package com.zgm.ch05.aop.pointcutAdvisor.annotationMatch._4_1_9_2;

import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

import com.zgm.ch05.aop.pointcutAdvisor.aspectJExpression._4_1_8.SimpleAfterAdvice;

public class AnnotationMatchingPointcutDemo {

	public static void main(String[] args) {
		SampleBean target = new SampleBean();
		SampleBean2 target2 = new SampleBean2();
		
//		AnnotationMatchingPointcut pc = new AnnotationMatchingPointcut(SimpleAnnotation.class, SimpleAnnotation.class);
//		AnnotationMatchingPointcut pc = new AnnotationMatchingPointcut(SimpleAnnotation.class, null);
		AnnotationMatchingPointcut pc = new AnnotationMatchingPointcut(null, SimpleAnnotation.class);
		
		SampleBean proxy = (SampleBean) getProxy(pc, target);
		proxy.getName();
		proxy.getHeight();
		proxy.setName("新Name");
		
		/////////
		System.out.println("\n\n==========================\n\n");
		
		SampleBean2 proxy2 = (SampleBean2) getProxy2(pc, target2);
		proxy2.getName();
		proxy2.getHeight();
		proxy2.setName("新Name");
		
	}
	
	public static SampleBean getProxy(AnnotationMatchingPointcut pc, SampleBean target) {
		Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAfterAdvice());
		ProxyFactory pf = new ProxyFactory();
		pf.addAdvisor(advisor);
		pf.setTarget(target);
		
		return (SampleBean) pf.getProxy();
		
	}
	
	public static SampleBean2 getProxy2(AnnotationMatchingPointcut pc, SampleBean2 target) {
		Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAfterAdvice());
		ProxyFactory pf = new ProxyFactory();
		pf.addAdvisor(advisor);
		pf.setTarget(target);
		
		return (SampleBean2) pf.getProxy();
		
	}
}
